<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<c-rt:set var="midContextPath" value='<%=ServerValue.getMidContextPath() %>'/>
<!-- 数字验证 -->
<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>
<script language="javascript">
	$().ready(function(){
		$("#submitButton").click(function() {			
			if (!validateBlockProducts()) {
				return false;
			}
			
			$("#blockProductForm").submit();
			return false;
		});

		$("#cancelCreateBlockProduct").click(function(){
			$("#createBlockProductDialog").dialog("close");
			return false;
		});

		$(":input[name*='productId']").each(function(){
			$("#" + this.id).numeric();
			$(this).blur(function(){
				$(this).val(prodPadLeft($(this).val()));
				var prefix = this.id+"-";
				var productName = prefix + "productName";
				var promoteName = prefix + "promoteName";
				var listPrice = prefix + "listPrice";
				var shelfPrice = prefix + "shelfPrice";
				var prodIsOK = prefix + "isOK";
				var graphicType = prefix + "graphicType";
				getProductInfoForActivity("${midContextPath}",productName,promoteName,listPrice,shelfPrice,prodIsOK,graphicType,$(this).val());
			});
		});

		checkEditPrice();
	});

	function checkEditPrice() {
		flag = $("#chkEditPrice").attr("checked");
		$(":input[name*='listPrice']").each(function(){
			$(this).attr("readonly",!flag);
			$(this).numeric();
		});
		$(":input[name*='shelfPrice']").each(function(){
			$(this).attr("readonly",!flag);
			$(this).numeric();
		});
	}

	var shelfPrice;
	var graphicType;
	function getProductInfoForActivity(path,productName,promoteName,listPrice,sPrice,pIsOk,pGraphicType,currentProdId){
		prodNameId = productName;
		prodBrief = promoteName;
		prodPrice = listPrice;
		shelfPrice = sPrice;
		prodIsOk = pIsOk;
		graphicType = pGraphicType
		$.post(path + "/generate/cateMainProd/findProductData.do",{'productId':currentProdId},function (jsonProduct){activityProductCallback(jsonProduct);},"json");
	}

	function activityProductCallback(jsonProduct){
		if ( null == jsonProduct ){
			$("#" + prodNameId).val("查无此商品");
			clearProduct();
		}else if ( jsonProduct.productType == 2 || jsonProduct.productType == 3 ){
			$("#" + prodNameId).val("赠品或加购商品无法被选为主打商品:"+jsonProduct.productType);
			clearProduct();
		}else if ( jsonProduct.isDelete == "Y"){
			$("#" + prodNameId).val("此商品已被删除");
			clearProduct();
		}else if ( jsonProduct.productStatus != "PRD500"){
			$("#" + prodNameId).val("商品状态不正确");
			clearProduct();
		}else{
			$("#" + prodIsOk).val("Y");
			//if(jsonProduct.productName.length>22)
			//	$("#" + prodNameId).val(jsonProduct.productName.substring(0,22));
			//else
				$("#" + prodNameId).val(jsonProduct.productName);
			if(jsonProduct.productBrief!=null){
				if(jsonProduct.productBrief.length>11)
					$("#" + prodBrief).val(jsonProduct.productBrief.substring(0,11));
				else
					$("#" + prodBrief).val(jsonProduct.productBrief);
			}else{
				$("#" + prodBrief).val('');
			}
			
			if(jsonProduct.productBrief=='商品已下架'){
				$("#" + prodNameId).val('商品已下架');
				clearProduct();
			} else {
				//if(jsonProduct.prpsShelfPrice!=null) {
					$("#" + prodPrice).val(jsonProduct.listPrice);
					if ($("#" + prodIsOk).val() == "Y") {
						$("#" + shelfPrice).val(jsonProduct.viewShelf.shelfPrice);
						$("#" + graphicType).val(jsonProduct.graphicType);
					}
				//}
			}
			
		}
	}

	function clearProduct(){
		$("#" + prodBrief).val("");
		$("#" + prodPrice).val("");
		$("#" + shelfPrice).val("");
		$("#" + prodIsOk).val("N");
		$("#" + graphicType).val("");
	}

	function validateBlockProducts() {
		if (!validateById("prdBlockTitle","商品区块标题文字")) return false;
		var isError = false;
		var pids = "";
		$(":input[name*='productId']").each(function(){
			if (!validateById(this.id,"商品编号")) {
				isError = true;
				return false;
			}
			var prefix = this.id+"-";
			var prodIsOK = prefix + "isOK";
			if ($("#"+prodIsOK).val() != "Y") {
				alert("商品编号错误"+$(this).val());
				$(this).focus();
				isError = true;
				return false;
			}
			if (pids.indexOf($(this).val()) != -1) {
				alert("商品编号重复");
				$(this).focus();
				isError = true;
				return false;
			} else {
				pids = pids + ";" + $(this).val();
			}
		});
		return !isError;
	}

	function validateById(id,inputDesc,fullMsg) {
		var obj = $("#"+id);
		if(obj.val() == '') {
			if (inputDesc != null) {
				alert("请输入"+inputDesc);
			} else if (fullMsg != '') {
				alert(fullMsg);
			}
			obj.focus();
			return false;
		}
		return true;
	}

</script>
<input type="hidden" id="template.id" value="${template.id}">
<div class="act-template-area">
    <!--- 组 -->
    <s:form id="blockProductForm" action="blockproduct/saveBlockProducts.do">
    	<s:hidden name="block.id"></s:hidden>
		<div class="${template.divClass}">
			<p>商品区块标题文字： 
			<s:textfield name="block.title" id="prdBlockTitle" size="55"></s:textfield>
			&nbsp;&nbsp;&nbsp;
			<input id="chkEditPrice" type="checkbox" value="1" onclick="checkEditPrice();">
			编辑价格
			</p>
			<br>
			<ul>
				<c-rt:forEach items="${blockProducts}" var="product" varStatus="status">
					<c:if test="${status.count <= template.bigImageCount}">
						<c:set var="liClass" value="first-one"></c:set>
					</c:if>
					<c:if test="${status.count > template.bigImageCount}">
						<c:set var="liClass" value=""></c:set>
					</c:if>
					<li class="${liClass}">
						商品编号：
						<input type="text" name="blockProducts[${status.count-1}].productId" id="prod${status.count-1}" value="${product.productId}">
						<input type="hidden" id="prod${status.count-1}-isOK" value="${product.valid}"/>
						<input type="hidden" name="blockProducts[${status.count-1}].graphicType" id="prod${status.count-1}-graphicType" value="${product.graphicType}">
						<em class="pficon">
							小标：
							<input type="text" name="blockProducts[${status.count-1}].promoteName" id="prod${status.count-1}-promoteName" value="${product.promoteName}">
						</em>
						<h6>
							商品標題：
							<input type="text" name="blockProducts[${status.count-1}].productName" id="prod${status.count-1}-productName" value="${product.productName}">
						</h6>
						<s>
							市场价
							<span>
								<input type="text" name="blockProducts[${status.count-1}].listPrice" id="prod${status.count-1}-listPrice" value="${product.listPrice}">
							</span>
						</s>
						<strong>
							会员价￥
							<input type="text" name="blockProducts[${status.count-1}].shelfPrice" id="prod${status.count-1}-shelfPrice" value="${product.shelfPrice}">
						</strong>
					</li>
				</c-rt:forEach>
			</ul>
			<div class="set-ft">&nbsp;</div>
		</div>
    </s:form>
	<!--- 组 -->
</div>
<p class="c-f l-link" align="center"><a href="#" id="submitButton" >確定</a> <a href="#" id="cancelCreateBlockProduct">取消</a></p>
